// pages/habit/habit.js
let util = require("../../utils/util.js");
const innerAudioContext = wx.getBackgroundAudioManager();
Page({
    //分享
    onShareAppMessage: function () { },
    /**
     * 页面的初始数据
     */
    data: {
        //轮播图属性设置
        indicatorDots: true,
        autoplay: false,
        interval: 3000,
        duration: 500,
        detail:{},
        comment_index:'aa',
        inputValue:"",//评论内容
        defaultComment:"评论",
        pageNumber: 1,
        readMoreFlag: false,
        imgHeight:0,
        audioData: {
            bookid: "",
            name: "",
            img: "",
            index: 0
        },
        isSign:"1"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var system = wx.getSystemInfoSync();
        var that = this;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        var audioData = wx.getStorageSync("audioData");
        if (!!audioData && audioData!=""){
            audioData = JSON.parse(audioData);
            this.setData({
                audioData: audioData
            });
        }
        this.setData({
            imgHeight: 300 * system.screenWidth / 750,
            userInfo: userInfo,
            left: system.windowWidth - 60,
            top: system.windowHeight - 60
        });
        this.getdetail();
    },
    //获取页面内容
    getdetail:function(){
        var userinfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        var that = this;
        util.wxAjax("glHabit/list","post",{
            adminid: userinfo
        },function(res){
            var data = res.data;
            for (let i = 0; i < data.data.habit.length; i++) {
                if (!!data.data.habit[i].img && data.data.habit[i].img != "") {
                    data.data.habit[i].img = data.data.habit[i].img.split("|");
                }
            }
            that.setData({
                detail: data.data,
                readMoreFlag:data.data.habit.length>19?true:false,
                isSign:data.data.isSign
            })
        })
    },
    //获取更多评论
    getMoreComment:function(){
        var that = this;
        var pageNum = this.data.pageNumber;
        var detail = this.data.detail;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        util.wxAjax("glHabit/moreHabit","post",{
            adminid:userInfo.smAdmin.id,
            pageNum: pageNum
        },function(res){
            var data = res.data;
            for(let i=0;i<data.data.length;i++){
                if (data.data[i].img != "" && !!data.data[i].img){
                    data.data[i].img = data.data[i].img.split("|");
                }
            }
            detail.habit = detail.habit.concat(data.data);
            that.setData({
                detail: detail,
                readMoreFlag:data.data.length>19?true:false
            });
        });
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // this.getdetail();
        this.checkPlayAudio();
    },
    //显示评论框
    show_comment(e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            inputValue: "",
            comment_index: !!index||index==0?index:"aa",
            defaultComment:"评论"
        });
    },
    //图片预览
    prevPicture: function (options) {
        var images = options.currentTarget.dataset.image;
        wx.previewImage({
            urls: images,
            success: function (res) {}
        });
    },
    //对评论点赞
    dianzan: function (e) {
        var id = e.currentTarget.dataset.id;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        var index = e.currentTarget.dataset.index;
        var detail = this.data.detail;
        let that = this;
        util.wxAjax("glHabit/updateLaudsAddOne", "post", {
            adminid: userInfo,
            id: id
        }, function (res) {
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                detail.habit[index].lauds = detail.habit[index].lauds*1+1;
                detail.habit[index].isLauds = 1;
                that.setData({
                    detail: detail
                });
            }else{
                wx.showToast({
                    title: data.msg,
                    icon:"none"
                })
            }
        })
    },
    payAttention:function(e){
        var publishId = e.currentTarget.dataset.id;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.id;
        if (publishId === userInfo){
            wx.showToast({
                title: '不能关注自己~',
                icon:"none"
            });
            return false;
        }
        var type = e.currentTarget.dataset.type;
        var detail = this.data.detail;
        var index = e.currentTarget.dataset.index;
        var url = "admin/addFollow";
        var that = this;
        if (type == "1" ||type == 1){
            url = "admin/cancelFollow";
        }
        util.wxAjax(url,"post",{
            adminid: userInfo,
            fansid: publishId
        },function(res){
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                var returMsg = type=="0"||type==0?"关注成功！":"取消关注成功！";
                wx.showToast({
                    title: returMsg
                });
                that.getdetail();
                // detail.habit[index].isFollow = type=="1"||type==1?"0":"1";
                // that.setData({
                //     detail:detail
                // });
            }else{
                wx.showToast({
                    title: data.msg,
                    icon:"none"
                })
            }
        })
    },
    //添加评论
    addComment:function(options){
        var username = JSON.parse(wx.getStorageSync("userInfo")).smAdmin.name;
        var id = options.currentTarget.dataset.id;
        var index = options.currentTarget.dataset.index;
        var inputValue = this.data.inputValue;
        var detail = this.data.detail;
        var toName = this.data.defaultComment;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        var that = this;
        if(!typeof(inputValue) || inputValue == ""){
            wx.showToast({
                title: '请输入评论内容！',
                icon:"none"
            })
            return false;
        }
        util.wxAjax("glHabit/updateCommentsAddOne","post",{
            fromName: username,
            toName: toName,
            id: id,
            content: inputValue,
            adminid:userInfo.smAdmin.id
        },function(res){
            var data = res.data;
            if(data.result == 200 || data.result == "200"){
                that.setData({
                    inputValue:"",
                    defaultComment:"评论"
                });
                detail.habit[index].habitcommon = detail.habit[index].habitcommon.concat(data.data);
                that.setData({
                    detail:detail
                });
            }else{
                wx.showToast({
                    title: data.msg,
                    icon:"none"
                })
            }
        })
    },
    //绑定input值
    setValue:function(options){
        var type = options.currentTarget.dataset.type;
        var value = options.detail.value;
        this.setData({
            inputValue:value
        });
    },
    //评论回复的评论
    pinglunRen:function(options){
        var name = options.currentTarget.dataset.name;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        if(name === userInfo.smAdmin.name){
            this.setData({
                defaultComment: "评论",
                inputValue: ""
            });
        }else{
            this.setData({
                defaultComment: name,
                inputValue: ""
            });
        }
    },
    //上拉刷新
    onPullDownRefresh: function () {
        var system = wx.getSystemInfoSync();
        // 显示顶部刷新图标  
        wx.showNavigationBarLoading();
        this.setData({
            pageNumber: 1,
            detail: {},
            left: system.windowWidth - 60,
            top: system.windowHeight - 60
        });
        this.getdetail();
    },
    //下拉加载
    onReachBottom: function () {
        var flag = this.data.readMoreFlag;
        if (flag) {
            var pageNum1 = this.data.pageNumber;
            // 显示加载图标  
            wx.showLoading({
                title: '玩命加载中',
            })
            this.setData({
                pageNumber: ++pageNum1,
            });
            this.getMoreComment();
        }
    },
    //删除动态
    deleteMsg:function(options){
        var that = this;
        var id = options.currentTarget.dataset.id;
        var index = options.currentTarget.dataset.index;
        var detail = this.data.detail;
        wx.showModal({
            title: '提示信息',
            content: '确认要删除该动态？',
            success:function(res){
                if(res.confirm){
                    util.wxAjax("glHabit/habitDelete","post",{
                        id: id
                    },function(res){
                        var data = res.data;
                        if(data.result == 200 || data.result == "200"){
                            detail.habit.splice(index,1);
                            wx.showToast({
                                title: '删除成功！',
                            })
                            that.setData({
                                detail:detail
                            });
                        }else{
                            wx.showToast({
                                title: data.msg,
                                icon:"none"
                            })
                        }
                    });
                }
            }
        })
    },
    //删除评论
    deleteComment: function (options) {
        var that = this;
        var id = options.currentTarget.dataset.id;
        var index = options.currentTarget.dataset.index;
        var parIndex = options.currentTarget.dataset.parindex;
        var detail = this.data.detail;
        var userInfo = JSON.parse(wx.getStorageSync("userInfo"));
        wx.showModal({
            title: '确认信息',
            content: '确认要删除当前评论吗？',
            success: function (res) {
                if(res.confirm){
                    util.wxAjax("glHabit/deleteCommon", "post", {
                        id: id,
                        adminid: userInfo.smAdmin.id
                    }, function (res) {
                        var data = res.data;
                        if (data.result == 200 || data.result == "200") {
                            detail.habit[parIndex].habitcommon.splice(index, 1);
                            that.setData({
                                detail: detail
                            });
                            wx.showToast({
                                title: '删除成功！',
                                icon: "none"
                            })
                        } else {
                            wx.showToast({
                                title: data.msg,
                                icon: "none"
                            })
                        }
                    })
                }
            }
        });
    },
    //判断是否有音乐在播放
    checkPlayAudio: function () {
        var time = innerAudioContext.currentTime;
        var bookid = wx.getStorageSync("listenBookId");
        var name = wx.getStorageSync("listenBookName");
        var img = wx.getStorageSync("listenBookImg");
        var index = wx.getStorageSync("listenBookIndex");
        var audioData = {};
        var that = this;
        setTimeout(function () {
            var newTime = innerAudioContext.currentTime;
            try {
                if (newTime >= time) {
                    audioData = {
                        bookid: bookid,
                        name: name,
                        img: img,
                        index: index
                    };
                } else {
                    var temp = wx.getStorageSync("audioData");
                    if (!!temp && temp != "") {
                        temp = JSON.parse(temp);
                    }
                    audioData = {
                        bookid: temp.bookid,
                        name: temp.name,
                        img: temp.img,
                        index: 0
                    };
                }
                that.setData({
                    audioData: audioData
                });
            } catch (e) {
                var temp = wx.getStorageSync("audioData");
                if (!!temp && temp != "") {
                    temp = JSON.parse(temp);
                    that.setData({
                        audioData: temp
                    });
                } else {
                    that.setData({
                        audioData: {
                            bookid: "",
                            name: "",
                            img: "",
                            index: 0
                        }
                    });
                }
            }
        }, 400);
    },
    viewTouchMove: function (e) {
        var system = wx.getSystemInfoSync();
        var left = e.touches[0].clientX;
        var top = e.touches[0].clientY;
        var width = system.windowWidth - 20;
        var height = system.windowHeight - 20;
        if (left < 0) {
            left = 0;
        }
        if (top < 0) {
            top = 0
        }
        if (width < left) {
            left = width;
        }
        if (height < top) {
            top = height;
        }
        this.setData({
            left: left - 25,
            top: top - 25
        })
    },
})